package com.qf.lanzhou.blog.api.dao.Impl;

import com.qf.lanzhou.blog.api.dao.ArticleDao;
import com.qf.lanzhou.blog.api.pojo.entity.Article;
import com.qf.lanzhou.blog.api.pojo.entity.Page;
import com.qf.lanzhou.blog.api.util.BeanProcessor;
import com.qf.lanzhou.blog.api.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Yeach-chen1
 * @ClassName ArticleDaoImp
 */
public class ArticleDaoImp implements ArticleDao {
    DataSource dataSource = DruidUtil.getDataSource();
    private QueryRunner queryRunner = new QueryRunner(dataSource);
    private static ArticleDaoImp articleDaoImp=new ArticleDaoImp();
    @Override
    public List<Article> getArticleList(String instr) {
        String sql = "SELECT * FROM t_article WHERE article_name like ? ";
        String instrAdd = "%" + instr + "%";
        BeanListHandler beanListHandler = BeanProcessor.getBeanListHandler(Article.class);
        List<Article> query = null;
        try {
            query = (List) queryRunner.query(sql, beanListHandler, instrAdd);
            System.out.println("后台提供的List" + query);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public List<Article> getAllArticleList() {
        String sql = "SELECT * FROM t_article";
        BeanListHandler beanListHandler = BeanProcessor.getBeanListHandler(Article.class);
        List<Article> query = null;
        try {
            query = (List<Article>) queryRunner.query(sql, beanListHandler);
            for (Article article:query) {
                article.setArticleProduce(article.getArticleCreatetime());
            }
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return query;
        }
    }

    @Override
    public int AddView(int num) {
        String sql = "UPDATE t_article SET article_view=article_view+1 WHERE article_id=? ";
        int result = 0;
        try {
            result = queryRunner.update(sql, num);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            return result;
        }
    }

    @Override
    public List<Article> getArticleListPage(String instr, Page page) {
        String sql = "SELECT * FROM t_article WHERE article_name like ? LIMIT ?,?;";
        Object[] prem = {"%" + instr + "%", page.getStartRow(), page.getPageSize()};
        BeanListHandler<Article> handler = BeanProcessor.getBeanListHandler(Article.class);
        try {
            List<Article> query = queryRunner.query(sql, handler, prem);
            for (Article article:query) {
                Long sumPrice = articleDaoImp.getSumPrice(article.getArticleId());
                article.setArticleLike(sumPrice);
            }
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public  Long getarticleListCount(String instr) {
        String sql = "SELECT count(*) FROM t_article WHERE article_name like ? ;";
        Object[] parm = {"%" + instr + "%"};
        ScalarHandler<Long> scalarHandler = new ScalarHandler<>();
        try {
            Long query = queryRunner.query(sql, scalarHandler, parm);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Article getArticleParam(int articleId) {
        System.out.println("catach the worong！！！！！！");
        String sql1 = "SELECT * FROM t_article WHERE article_id=? ";
        String sql2 = "SELECT SUM(praise_status) FROM t_praise WHERE article_id=? GROUP BY article_id;";
        //通过ID获取文章对象
        BeanHandler beanHandler = BeanProcessor.getBeanHandler(Article.class);
        Article query=null;
        Double query1=0.0;
        try {
          query = (Article)queryRunner.query(sql1, beanHandler, articleId);
            System.out.println("查看query::"+query);
            query1 = queryRunner.query(sql2, new ScalarHandler<Double>(), articleId);
            if(query1!=null){
                System.out.println(query1.toString());
                System.out.println("查看query"+query.getArticleId()+"点赞数："+query.getArticleLike());

                String str = query1.toString();
                String substring = str.substring(0, str.length() - 2);
                System.out.println(substring);
                int sum = Integer.parseInt(substring);
                query.setArticleLike(sum);
                return query;
            }
            query.setArticleLike(0);
          return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }   return null;
    }

    @Override
    public Long getArticleStatus(Long userId, Long ArticleId) {
        String sql = "SELECT praise_status FROM t_praise WHERE user_id=? AND article_id=?;";
         Object[] params={userId,ArticleId};
        String Articlestatus=null;
        try {
            Articlestatus=queryRunner.query(sql,new ScalarHandler<String>(),params);
          /*  System.out.println(Articlestatus+"sb键。。。。。。。。");*/
            if (Articlestatus==null||Articlestatus.equals("0")){
               /* Articlestatus = Long.decode(substring);
                System.out.println("点数状态---："+ArticleId);*/
                return 0L;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 1L;
    }

    @Override
    public  Long getSumPrice(Long articleId) {
        String sql2 = "SELECT SUM(praise_status) FROM t_praise WHERE article_id=? GROUP BY article_id;";
        Double sum=0.0;
        long articlePrice=0L;
        try {
            sum=queryRunner.query(sql2,new ScalarHandler<Double>(),articleId);
            if (sum!=null){
                String str = sum.toString();
                String substring = str.substring(0, str.length() - 2);
                articlePrice = Long.parseLong(substring);
                return articlePrice;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return articlePrice;
    }
    public static ArticleDaoImp getInstance(){
        return articleDaoImp;
    }
}
